﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Extension;
using NXDO.Data.Factory;
using NXDO.Data.Partition;
using NXDO.WebAPI.Controllers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using TDM.DbData.DataEntity;
using TDM.WebBase;
using TDM.WebBiz.Common;
using TDM.WebBiz.Project;
using TDM.WebBiz.TestData;

namespace TDM.WebSvr.Controllers.TestData.File
{
    [AllowAnonymous]
    public class TestTaskFileUpDnldController : ApiBinaryController
    {
        /// <summary>
        /// 上传数据
        /// </summary>
        protected UploadData uploadData;
        protected override string GetSaveFileName(string name, bool nameIsMimeContentType)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            //获取基础地址
            string projectPath = BasePathHelper.GetProjectBasePath();
            var taskid = uploadData.getID().Value;
            //获取项目
            var task = TestTaskHelper.Get(taskid);
            if (task == null) throw new Exception("未找到对应数据");
            //按规则拼存储地址
            long id = DbGenerateSnowKey.GetSnowKey(1, 1);
            if (!uploadData.filetype.HasValue) throw new Exception("未标识文件类型");
            int fileType = uploadData.filetype.Value;
            var fileTypePath = (TaskFileType)fileType;
            var storePath = Path.Combine(task.PrjID.ToString(), task.TaskID.ToString(), "TaskFile", fileTypePath.ToString());//\Project\{PrjID}\{TaskID}\ResultFile\{ResultID}\{FileType}\{FileID.*}

            TestTaskFile file = new()
            {
                TaskFID = id,
                TaskID = taskid,
                StorePath = storePath,
                FileExtension = Path.GetExtension(name),
                FileName = name,
                CreateUID = uid,
                CreateTime = DateTime.Now
            };
            file.DBLinq().ToCommand(file).Insert();
            var fullPath = Path.Combine(projectPath, storePath, id + Path.GetExtension(name));
            if (System.IO.File.Exists(fullPath)) throw new Exception("文件名称重复");
            return fullPath;
        }

        public override object Upload([FromRoute] string name)
        {
            uploadData = JsonSerializer.Deserialize<UploadData>(name);
            return base.Upload(name);
        }


        protected override Stream GetDownloadStream(string name)
        {
            string projectPath = BasePathHelper.GetProjectBasePath();
            long fileID = Convert.ToInt64(name);
            TestTaskFile file = TestTaskFileHelper.Get(fileID);
            var filePath = Path.Combine(projectPath, file.StorePath, fileID.ToString() + file.FileExtension);
            Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            return stream;
        }
    }
}
